def byteReverse(strData):
    if len(strData) % 2 != 0:
        raise Exception('the length of data needs to be even')
    firstStr = strData[-2:: -2]
    secondStr = strData[-1:: -2]
    combinedStr = ''
    for i in range(len(firstStr)):
        combinedStr += firstStr[i] + secondStr[i]
    return combinedStr


def str2int(strData):
    bigStr = ''
    for i in range(len(strData)):
        bigStr += str(int(strData[i], 16) - 3)
    return int(bigStr)


def getOrder(strData):
    orderList = ['33323333', '33323433', '33323533', '35459337', '34459337']
    return orderList.index(strData[28: 36].lower())


def getData(strData):
    return strData[36: -4]


def parsingFirstData(strData, results):
    results['组合有功总电能'] = str2int(byteReverse(strData[0: 8])) / 100.0
    results['组合有功总电能尖'] = str2int(byteReverse(strData[8: 16])) / 100.0
    results['组合有功总电能峰'] = str2int(byteReverse(strData[16: 24])) / 100.0
    results['组合有功总电能平'] = str2int(byteReverse(strData[24: 32])) / 100.0
    results['组合有功总电能谷'] = str2int(byteReverse(strData[32: 40])) / 100.0


def parsingSecondData(strData, results):
    results['正向有功总电能'] = str2int(byteReverse(strData[0: 8])) / 100.0
    results['正向有功总电能尖'] = str2int(byteReverse(strData[8: 16])) / 100.0
    results['正向有功总电能峰'] = str2int(byteReverse(strData[16: 24])) / 100.0
    results['正向有功总电能平'] = str2int(byteReverse(strData[24: 32])) / 100.0
    results['正向有功总电能谷'] = str2int(byteReverse(strData[32: 40])) / 100.0


def parsingThirdData(strData, results):
    results['反向有功总电能'] = str2int(byteReverse(strData[0: 8])) / 100.0
    results['反向有功总电能尖'] = str2int(byteReverse(strData[8: 16])) / 100.0
    results['反向有功总电能峰'] = str2int(byteReverse(strData[16: 24])) / 100.0
    results['反向有功总电能平'] = str2int(byteReverse(strData[24: 32])) / 100.0
    results['反向有功总电能谷'] = str2int(byteReverse(strData[32: 40])) / 100.0


def parsingFourthData(strData, results):
    results['瞬时总有功功率'] = str2int(byteReverse(strData[0: 6])) / 10000.0
    results['瞬时A相有功功率'] = str2int(byteReverse(strData[6: 12])) / 10000.0
    results['瞬时B相有功功率'] = str2int(byteReverse(strData[12: 18])) / 10000.0
    results['瞬时C相有功功率'] = str2int(byteReverse(strData[18: 24])) / 10000.0
    results['总功率因数'] = str2int(byteReverse(strData[24: 28])) / 1000.0
    results['A相功率因数'] = str2int(byteReverse(strData[28: 32])) / 1000.0
    results['B相功率因数'] = str2int(byteReverse(strData[32: 36])) / 1000.0
    results['C相功率因数'] = str2int(byteReverse(strData[36: 40])) / 1000.0
    results['状态字'] = str2int(byteReverse(strData[40: 42]))


def parsingFifthData(strData, results):
    results['A相电压'] = str2int(byteReverse(strData[0: 4])) / 10.0
    results['B相电压'] = str2int(byteReverse(strData[4: 8])) / 10.0
    results['C相电压'] = str2int(byteReverse(strData[8: 12])) / 10.0
    results['A相电流'] = str2int(byteReverse(strData[12: 18])) / 1000.0
    results['B相电流'] = str2int(byteReverse(strData[18: 24])) / 1000.0
    results['C相电流'] = str2int(byteReverse(strData[24: 30])) / 1000.0


def parsing(dataList):
    results = {}
    for data in dataList:
        order = getOrder(data)
        if order == 0:
            parsingFirstData(getData(data), results)
        elif order == 1:
            parsingSecondData(getData(data), results)
        elif order == 2:
            parsingThirdData(getData(data), results)
        elif order == 3:
            parsingFourthData(getData(data), results)
        else:
            parsingFifthData(getData(data), results)
    return results


def mapping(results):
    mappingResults = {}
    mappingResults['组合有功总电能'] = ('%.2f' + 'KW/h') % results['组合有功总电能']
    mappingResults['组合有功总电能尖'] = ('%.2f' + 'KW/h') % results['组合有功总电能尖']
    mappingResults['组合有功总电能峰'] = ('%.2f' + 'KW/h') % results['组合有功总电能峰']
    mappingResults['组合有功总电能平'] = ('%.2f' + 'KW/h') % results['组合有功总电能平']
    mappingResults['组合有功总电能谷'] = ('%.2f' + 'KW/h') % results['组合有功总电能谷']
    mappingResults['正向有功总电能'] = ('%.2f' + 'KW/h') % results['正向有功总电能']
    mappingResults['正向有功总电能尖'] = ('%.2f' + 'KW/h') % results['正向有功总电能尖']
    mappingResults['正向有功总电能峰'] = ('%.2f' + 'KW/h') % results['正向有功总电能峰']
    mappingResults['正向有功总电能平'] = ('%.2f' + 'KW/h') % results['正向有功总电能平']
    mappingResults['正向有功总电能谷'] = ('%.2f' + 'KW/h') % results['正向有功总电能谷']
    mappingResults['反向有功总电能'] = ('%.2f' + 'KW/h') % results['反向有功总电能']
    mappingResults['反向有功总电能尖'] = ('%.2f' + 'KW/h') % results['反向有功总电能尖']
    mappingResults['反向有功总电能峰'] = ('%.2f' + 'KW/h') % results['反向有功总电能峰']
    mappingResults['反向有功总电能平'] = ('%.2f' + 'KW/h') % results['反向有功总电能平']
    mappingResults['反向有功总电能谷'] = ('%.2f' + 'KW/h') % results['反向有功总电能谷']
    mappingResults['瞬时总有功功率'] = ('%.4f' + 'KW') % results['瞬时总有功功率']
    mappingResults['瞬时A相有功功率'] = ('%.4f' + 'KW') % results['瞬时A相有功功率']
    mappingResults['瞬时B相有功功率'] = ('%.4f' + 'KW') % results['瞬时B相有功功率']
    mappingResults['瞬时C相有功功率'] = ('%.4f' + 'KW') % results['瞬时C相有功功率']
    mappingResults['总功率因数'] = ('%.3f') % results['总功率因数']
    mappingResults['A相功率因数'] = ('%.3f') % results['A相功率因数']
    mappingResults['B相功率因数'] = ('%.3f') % results['B相功率因数']
    mappingResults['C相功率因数'] = ('%.3f') % results['C相功率因数']
    mappingResults['状态字'] = ('%02d') % results['状态字']
    mappingResults['A相电压'] = ('%.1f' + 'V') % results['A相电压']
    mappingResults['B相电压'] = ('%.1f' + 'V') % results['B相电压']
    mappingResults['C相电压'] = ('%.1f' + 'V') % results['C相电压']
    mappingResults['A相电流'] = ('%.3f' + 'A') % results['A相电流']
    mappingResults['B相电流'] = ('%.3f' + 'A') % results['B相电流']
    mappingResults['C相电流'] = ('%.3f' + 'A') % results['C相电流']
    return mappingResults